# coding: utf-8
#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jiafu_server.settings')
    import datetime
    from util.jiafu_log import JiaFuLogger
    from util.const import LOG_DIR

    cur_dir = os.path.dirname(os.path.realpath(__file__))
    log_dir = os.path.join(cur_dir, LOG_DIR)

    if not os.path.exists(log_dir):
        os.mkdir(log_dir)
    str_dt2 = datetime.datetime.now().strftime("%Y-%m-%d")
    file_name = os.path.join(log_dir, 'jiafu_server_%s.log' % str_dt2)
    logger = JiaFuLogger("jiafu_server:manage", filename=file_name)
    # 当manager启动时，恢复之前未完成的服务器条件单，注意django启动命令需要加 --noreload参数，避免重复执行两次
    try:
        if sys.argv[1] == "runserver":
            from condition_order.services.condition_order_logic import recover_condition_order
            logger.debug("do recover condition order")
            recover_condition_order()
    except Exception as e:
        logger.error(str(e))

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        )
    execute_from_command_line(sys.argv)



